import Vue from 'vue';
import Axios from 'axios';
import iView from 'iview';
import {router} from './router/index';
import {appRouter} from './router/router';
import store from './store';
import App from './app.vue';
import '@/locale';
import 'iview/dist/styles/iview.css';
import VueI18n from 'vue-i18n';
import util from './libs/util';
import config from './utils/config';
import {get_local_user_token} from './utils/config_common';
import config_api from '@/utils/config_api.js';
import {formatTime} from '@/utils/config_common.js';


Vue.use(VueI18n);
Vue.use(iView);
/**  =========== Axios =============*/
Axios.defaults.baseURL = config.api_host;
Axios.defaults.responseType = 'json';
Axios.defaults.headers = {
    // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Content-Type': 'application/json'
};
Axios.defaults.validateStatus = function (status) {
    return true;
};

Axios.interceptors.request.use(config => {
    var dogo_token = get_local_user_token();
    if (dogo_token != '') {
        config.headers['Authorization'] = dogo_token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});
// 响应时拦截
Axios.interceptors.response.use(response => {
    // 关闭加载数据提示信息
    // console.log('响应时拦截ok', response);
    return response;
}, error => {
     // 返回 response 里的错误信息
     // console.log('响应时拦截error', error.response);
    return Promise.reject(error.response);
});

Vue.prototype.$axios = Axios;
Vue.prototype.$configApi = config_api;
Vue.prototype.$hostUrl = config;
Vue.prototype.$formatTime = formatTime;

Vue.prototype.$getMainColor = () => {
    let theme = localStorage.getItem('areaTheme') || '[]';
    theme = JSON.parse(theme)[0] || {};
    const {element = '#108cee'} = theme;
    return element;
};


Vue.prototype.$checkAccess = (accessName) => {
    cook
};

Vue.prototype.$clearCache = function (name = '') {
    this.$store.commit('closePage', name);
    this.$store.commit('removeTag', name);
};

/** ===========Axios =============*/
Vue.config.productionTip = false;
new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App),
    data: {
        areaCurrentPageName: ''
    },
    mounted() {
        this.areaCurrentPageName = this.$route.name;
        // 显示打开的页面的列表
        this.$store.commit('setOpenedList');
        this.$store.commit('initCachepage');
        // 权限菜单过滤相关
        this.$store.commit('updateMenulist');
        // iview-admin检查更新
        util.checkUpdate(this);
    },
    created() {
        let tagsList = [];
        appRouter.map((item) => {
            if (item.children.length <= 1) {
                tagsList.push(item.children[0]);
            } else {
                tagsList.push(...item.children);
            }
        });
        this.$store.commit('setTagsList', tagsList);
        // console.log(this.$store)
    }
});
